home *** CD-ROM | disk | FTP | other *** search
- Path: faatcrl.faa.gov!saratoga!lbona
- From: lbona@saratoga (lbona)
- Newsgroups: comp.lang.c
- Subject: Re: returning ptr to struct with ptrs in it?
- Date: 23 Feb 1996 21:50:53 GMT
- Organization: FAA Technical Center, Pomona, NJ
- Message-ID: <4glcrtINNf4b@faatcrl.faa.gov>
- References: <4gk852INNbp4@faatcrl.faa.gov>
- Reply-To: lbona@tgf.tc.faa.gov
- NNTP-Posting-Host: 155.178.247.116
- X-Newsreader: Tin 1.1 PL4
-
- lbona@saratoga (lbona) writes:
- :
- : The following is beyond the scope of my knowledge. Perhaps someone can explain
- : it to me:
- :
- : When I declare a function that returns a pointer to a struct that contains
- : pointers, the pointer(s) at the end of the struct get set to garbage after
- : being returned. Pointers not at the end are not affected.
-
-
- When I posted this question I had just finished an all-night coding
- session and my brain was mush. As a result my question wasn't as
- clear as I thought, actually it was misleading.
-
- What I was trying to get at was this: When I declare a local variable,
- bb, I know it's using temp memory off the stack. When the variable goes
- out of scope, i.e. leaving the function, the memory is returned to the
- stack. However, it is not cleared (compiler dependant of course), the
- data stays the same until something else needs the memory. This is why
- all the fields in the struct still have the correct values and I can
- assign them to a struct, BB, of the same type that is still extant.
-
- My real question is: why is it that if the struct ends with a pointer
- or pointers, that area of memory is corrupted (made non-zero in this case)
- yet if the struct ends in a non-pointer field the area of memory in
- question is not changed? All the data is the same, including pointers in
- the middle of the struct.
-
- Anyway, as I said in my original post, I know it's better to pass the
- address of the struct in question to the function, and that's what I'll
- be doing, but this struck me as strange, and I was curious as to what
- was happening.
-
-
- : --------------------------------------------------------------
- :
- : typedef struct aaa {
- : int i;
- : char name[22];
- : int id;
- : } AAA;
- :
- : typedef struct bar {
- : long a;
- : long b;
- : int c;
- : AAA *d;
- : char e;
- : char f[100];
- : AAA *g;
- : AAA *h;
- : } BAR;
- :
- : BAR *parse_bar(char toke[]);
- :
- : main()
- : {
- : BAR BB;
- : char toke[128];
- :
- : memset(&BB,0,sizeof(BAR));
- : /* at this point BB.d == BB.g == BB.h == NULL */
- :
- : /* toke is an ascii string read in from a file */
- : BB = *parse_bar(toke);
- :
- : /* at this point BB.g and BB.h are garbage, but all the other fields */
- : /* in BB are correct including BB.d*/
- :
- : }
- :
- :
- : BAR *parse_bar(char toke[]);
- : {
- : BAR bb;
- :
- : memset(&bb,0,sizeof(BAR));
- : /* at this point BB.d == BB.g == BB.h == NULL */
- :
- : /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
- :
- : /* at this point BB.d == BB.g == BB.h == NULL */
- : return(&bb);
- : }
- :
- :
- : ----------------------------------------------------------
- :
- : I know there are several work arounds, and that it's better to pass the address
- : of BB to parse_bar(), which is what I'll be doing. But I'm curious why this is
- : happening. I've tried it under K&R on Unix and ANSI on DOS and the results
- : were identical.
- :
- :
- : Any help would be appreciated,
- :
- : thanks
- :
- : --
- :
- : Lou Bona
- : lbona@tgf.tc.faa.gov
- :
-
- --
-
- Lou Bona
- lbona@tgf.tc.faa.gov
-
-